<!doctype html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>jQuery响应式可拖拽的元素组件网格布局插件</title>
	<link rel="stylesheet" href="http://libs.baidu.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css"/>
    <link rel="stylesheet" href="dist/gridstack.css"/>
	<link rel="stylesheet" type="text/css" href="css/default.css">
	<style type="text/css">
        .grid-stack {
            background: lightgoldenrodyellow;
        }

        .grid-stack-item-content {
            color: #2c3e50;
            text-align: center;
            background-color: #18bc9c;
        }
    </style>
	<!--[if IE]>
		<script src="http://libs.baidu.com/html5shiv/3.7/html5shiv.min.js"></script>
	<![endif]-->
</head>
<body>
	<div class="jq22-container">
		<header class="jq22-header">
			<h1>jQuery响应式可拖拽的元素组件网格布局插件 <span>A jQuery plugin for widget layout</span></h1>
			
            <div class="jq22-demo center">
              <a href="index.html">float</a>
              <a href="knockout.html">knockout</a>
              <a href="knockout2.html" class="current">knockout2</a>
              <a href="nested.html">nested</a>
              <a href="serialization.html">serialization</a>
            </div>
		</header>
		<div class="jq22-content bgcolor-3">
			<div class="container-fluid">
		        <h1>knockout.js Demo2</h1>

                <div>
                    <button data-bind="click: add_new_widget">Add new widget</button>
                </div>

                <br>

                <div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
		    </div>
	    </div>
		
	</div>
	
	<script src="http://www.jq22.com/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://libs.baidu.com/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="js/lodash.min.js"></script>
    <script src="js/knockout-min.js"></script>
    <script src="dist/gridstack.js"></script>
    <script type="text/javascript">
        ko.components.register('dashboard-grid', {
            viewModel: {
                createViewModel: function (controller, componentInfo) {
                    var ViewModel = function (controller, componentInfo) {
                        var grid = null;

                        this.widgets = controller.widgets;

                        this.afterAddWidget = function (items) {
                            if (grid == null) {
                                grid = $(componentInfo.element).find('.grid-stack').gridstack({
                                    auto: false
                                }).data('gridstack');
                            }

                            var item = _.find(items, function (i) { return i.nodeType == 1 });
                            grid.add_widget(item);
                            ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
                                grid.remove_widget(item);
                            });
                        };
                    };

                    return new ViewModel(controller, componentInfo);
                }
            },
            template: { element: 'gridstack-template' }
        });

        $(function () {
            var Controller = function (widgets) {
                var self = this;

                this.widgets = ko.observableArray(widgets);

                this.add_new_widget = function () {
                    this.widgets.push({
                        x: 0,
                        y: 0,
                        width: Math.floor(1 + 3 * Math.random()),
                        height: Math.floor(1 + 3 * Math.random()),
                        auto_position: true
                    });
                };

                this.delete_widget = function (item) {
                    self.widgets.remove(item);
                };
            };

            var widgets = [
                {x: 0, y: 0, width: 2, height: 2},
                {x: 2, y: 0, width: 4, height: 2},
                {x: 6, y: 0, width: 2, height: 4},
                {x: 1, y: 2, width: 4, height: 2}
            ];

            var controller = new Controller(widgets);
            ko.applyBindings(controller);
        });
    </script>
    <template id="gridstack-template">
        <div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">
           <div class="grid-stack-item" data-bind="attr: {'data-gs-x': $data.x, 'data-gs-y': $data.y, 'data-gs-width': $data.width, 'data-gs-height': $data.height, 'data-gs-auto-position': $data.auto_position}">
               <div class="grid-stack-item-content"><button data-bind="click: $root.delete_widget">Delete me</button></div>
           </div></div><!-- <---- NO SPACE BETWEEN THESE CLOSING TAGS -->
    </template>
</body>
</html>